/************************************************************************
* main.c
* Copyright (C) Lisa Milne 2013 <lisa@ltmnet.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
************************************************************************/

#include "dish.h"
#include "env.h"

static int estate = 0;

/* set the shell's exit state */
void exit_state(int n)
{
	estate = n;
}

int main(int argc, char** argv)
{
	int c;
	int s = 0;
	char* optstring = ":sic:";

	env_reload();
	while ((c = getopt(argc, argv, optstring)) != -1) {
		switch(c) {
		case 's':
			s = 1;
			break;
		case 'i':
			break;
		case 'c':
			estate = parse_line(optarg,STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO);
			break;
		case ':':	/* missing operand */
			fprintf(
				stderr,
				"Option -%c: missing operand\n",
				optopt
			);
			return 1;
		break;
		case '?':
			usage();
			return 0;
			break;
		default:;
		}
	}

	if (!s && isatty(STDIN_FILENO)) {
		parse_interactive(STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO);
	}else{
		estate = parse_stream(STDIN_FILENO,STDOUT_FILENO,STDERR_FILENO);
	}

	return estate;
}
